package org.study.neo4j.boot.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.study.neo4j.boot.model.*;
import org.study.neo4j.boot.repository.*;

/**
 * 作用描述
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2025年04月09日
 */
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Service
public class RelationshipService {
    private final StudentRepository studentRepository;
    private final TeacherRepository teacherRepository;
    private final CourseRepository courseRepository;

    @Transactional
    public void enrollStudentInCourse(Long studentId, String courseCode) {
        Student student = studentRepository.findById(studentId).orElseThrow();
        Course course = courseRepository.findByCode(courseCode);
        student.getCourses().add(course);
        studentRepository.save(student);
    }

    @Transactional
    public void assignTeacherToCourse(Long teacherId, String courseCode) {
        Teacher teacher = teacherRepository.findById(teacherId).orElseThrow();
        Course course = courseRepository.findByCode(courseCode);
        course.setTeacher(teacher);
        courseRepository.save(course);
    }

    @Transactional
    public void assignAdvisorToStudent(Long teacherId, Long studentId) {
        Teacher teacher = teacherRepository.findById(teacherId).orElseThrow();
        Student student = studentRepository.findById(studentId).orElseThrow();
        student.getTeachers().add(teacher);
        studentRepository.save(student);
    }

    @Transactional
    public void removeStudentFromCourse(Long studentId, String courseCode) {
        Student student = studentRepository.findById(studentId).orElseThrow();
        Course course = courseRepository.findByCode(courseCode);
        student.getCourses().remove(course);
        studentRepository.save(student);
    }
}
